#include "StdAfx.h"
#include "JG_FixTardeData.h"
#include "JG_FixCounterContext.h"


JG_FixCounterContext::JG_FixCounterContext(void)
	:m_pCJGReqDataApi(NULL),m_nConnectID(-1),m_lOCClock(0)
{
	m_pCJGReqDataApi = new CJG_ReqDataApi;
	ZeroMemory(&m_struCounter,sizeof(FG_struCounter));

	JG_CltSetConnectStatusCallBackFun(ConnectStatus);
	JG_CLTREQDATA_SetTradeRecvDataCallBackFun(TradeDataReceive);
}


JG_FixCounterContext::~JG_FixCounterContext(void)
{
	if( NULL != m_pCJGReqDataApi )
	{
		//delete m_pCJGReqDataApi;
		//m_pCJGReqDataApi = NULL;
	}
}

int JG_FixCounterContext::AddCounterToQueIfNotExist(FG_struCounter struCounter)
{
	CJGCriticalLock _csCounterContext(&JG_FixCounterContext::s_csCounterQueLock);

	BOOL bFound = FALSE;
	for( std::vector<JG_FixCounterContext*>::iterator iterCounter = JG_FixCounterContext::s_CounterQue.begin();\
		 iterCounter != JG_FixCounterContext::s_CounterQue.end(); )
	{
		JG_FixCounterContext *pFGCounterContext = (JG_FixCounterContext*)(*iterCounter);

		if( pFGCounterContext->m_struCounter.nSerialNo == struCounter.nSerialNo )
		{
			bFound = TRUE;
			break;
		}
		else
		{
			iterCounter++;
		}
	}

	if( bFound == FALSE )
	{
		JG_FixCounterContext* pFGCounterContext = new JG_FixCounterContext;
		memcpy(&pFGCounterContext->m_struCounter, &struCounter, sizeof(FG_struCounter));
		JG_FixCounterContext::s_CounterQue.push_back(pFGCounterContext);
	}

	return S_OK;
}

int JG_FixCounterContext::RemoveCounterContextQue()
{
	CJGCriticalLock _csCounterContext(&JG_FixCounterContext::s_csCounterQueLock);

	for( std::vector<JG_FixCounterContext*>::iterator iterCounter = JG_FixCounterContext::s_CounterQue.begin();\
		 iterCounter != JG_FixCounterContext::s_CounterQue.end(); )
	{
		JG_FixCounterContext *pCounterContext = (JG_FixCounterContext*)(*iterCounter);

		if( NULL != pCounterContext )
		{
			delete pCounterContext;
			pCounterContext = NULL;		
		}

		iterCounter = JG_FixCounterContext::s_CounterQue.erase(iterCounter);
	}

	return S_OK;
}